package org.boulderfoodrescue.webapp.client.options.myinfo;

import org.boulderfoodrescue.webapp.client.managers.UserAccountManager;
import org.boulderfoodrescue.webapp.client.options.OptionsInterface;
import org.boulderfoodrescue.webapp.shared.UserAccount;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class MyInfoPanel extends Composite {

	private static MyinfoPanelUiBinder uiBinder = GWT
			.create(MyinfoPanelUiBinder.class);

	interface MyinfoPanelUiBinder extends UiBinder<Widget, MyInfoPanel> {

	}

	public MyInfoPanel(UserAccount user, OptionsInterface optionsInterface) {
		this.optionsInterface = optionsInterface;
		initWidget(uiBinder.createAndBindUi(this));
		if(user != null)
			setUserAccountData(user);
	}

	@UiField
	TextBox firstName;
	@UiField
	TextBox lastName;
	@UiField
	TextBox address;
	@UiField
	TextBox city;
	@UiField
	ListBox state;
	@UiField
	TextBox zipCode;
	@UiField
	TextBox phoneNumber;
	@UiField
	Button submit;
	@UiField
	Label passcode;
	@UiField
	TextBox gmail;
	@UiField
	TextBox alternativeEmail;
	@UiField
	ListBox preferredMethodOfContact;
	private UserAccount user;
	private OptionsInterface optionsInterface;

	public void setUserAccountData(UserAccount user)
	{
		this.user = user;
		address.setText(user.getAddress());
		city.setText(user.getCity());
		state.setSelectedIndex(Integer.parseInt(""+user.getState()));
		zipCode.setText(user.getZipCode());
		firstName.setText(user.getFirstName());
		lastName.setText(user.getLastName());
		phoneNumber.setText(user.getPhoneNumber());
		passcode.setText(user.getPasscode());
		gmail.setText(user.getEmail());
		alternativeEmail.setText(user.getAlternateEmail());
		preferredMethodOfContact.setSelectedIndex(user.getPreferedContactMethodAsLong().intValue());
	}



	@UiHandler("submit")
	public void onSubmitClick(ClickEvent e){
		if(user == null || user.getDatabaseKey() == null)
		{
			Log.error("User is null");
			return;
		}
		submit.setEnabled(false);
		submit.setText("Please wait...");
		user.setAddress(address.getText());
		user.setCity(city.getText());
		user.setState(Long.parseLong(state.getValue(state.getSelectedIndex())));
		user.setZipCode(zipCode.getText());
		user.setFirstName(firstName.getText());
		user.setLastName(lastName.getText());
		user.setPhoneNumber(phoneNumber.getText());
		user.setAlternateEmail(alternativeEmail.getText());
		user.setPreferedContactMethodViaLong(new Long(preferredMethodOfContact.getSelectedIndex()));

		UserAccountManager.updateUser(user);
	}

	public void onUpdateMyInfoComplete()
	{
		submit.setEnabled(true);
		submit.setText("Save");
	}
}
